import request from '@/utils/request'

export function login(data: Global.LoginUser) {
  return request({
    url: '/permission/connect/token',
    method: 'post',
    data
  })
}
export function getPermissionsByUser(user: Global.User) {
  return request({
    url: '/permission/api/app/permission',
    method: 'get',
    params: {
      providerName: 'U',
      providerKey: user.sub
    }
  })
}
export function getAllRole() {
  return request({
    url: '/permission/api/identity/users/assignable-roles',
    method: 'get'
  })
}
export function getPermissions(role: Global.Role) {
  return request({
    url: '/permission/api/permission-management/permissions',
    method: 'get',
    params: {
      providerName: 'R',
      providerKey: role.name
    }
  })
}
export function setPermissions(role: Global.Role, data) {
  return request({
    url: '/permission/api/permission-management/permissions',
    method: 'put',
    params: {
      providerName: 'R',
      providerKey: role.name
    },
    data
  })
}
//角色增删改查
export function getRoles(data: Global.TableParams) {
  return request({
    url: '/permission/api/identity/roles',
    method: 'get',
    params: data
  })
}
export function editRole(data: Global.Role) {
  return request({
    url: `/permission/api/identity/roles/${data.id}`,
    method: 'put',
    data
  })
}
export function createRole(data: Global.Role) {
  return request({
    url: '/permission/api/identity/roles',
    method: 'post',
    data
  })
}
export function deleteRole(data: Global.Role) {
  return request({
    url: `/permission/api/identity/roles/${data.id}`,
    method: 'delete'
  })
}
//用户增删改查
export function getUsers(data: Global.TableParams) {
  return request({
    url: '/permission/api/app/identityUser',
    method: 'get',
    params: data
  })
}
export function editUser(data: Global.User) {
  return request({
    url: `/permission/api/identity/users/${data.id}`,
    method: 'put',
    data
  })
}
export function createUser(data: Global.User) {
  return request({
    url: '/permission/api/identity/users',
    method: 'post',
    data
  })
}
export function deleteUser(data: Global.User) {
  return request({
    url: `/permission/api/identity/users/${data.id}`,
    method: 'delete'
  })
}
export function setUserRole(user: Global.User,data: {roleNames: string[]}) {
  return request({
    url: `/permission/api/identity/users/${user.id}/roles`,
    method: 'put',
    data
  })
}